<template>
	<section class="app-container">
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters" @submit.native.prevent>
        <el-form-item label="账户状态">
          <el-select v-model="filters.region" placeholder="请选择">
           <el-option label="启动" value="1"></el-option>
            <el-option label="禁用" value="2"></el-option>
          </el-select>
        </el-form-item>
				<el-form-item label="登录账号">
					<el-input v-model="filters.phone" placeholder="请输入登录账户"></el-input>
				</el-form-item>
        <el-form-item label="联系人">
        	<el-input v-model="filters.name" placeholder="请输入联系人"></el-input>
        </el-form-item>
				<el-form-item>
					<el-button type="danger">查询</el-button>
				</el-form-item>
				<el-form-item style="position: absolute;right: 70px;z-index: 99;">
					<el-button type="danger" size="small " @click="handleAdd">添加账户</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="users" highlight-current-row @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column prop="name" label="序号" width="120">
			</el-table-column>
			<el-table-column prop="name" label="账户ID" width="130">
			</el-table-column>
			<el-table-column prop="name" label="登录账号" width="150">
			</el-table-column>
			<el-table-column prop="name" label="联系人" width="150">
			</el-table-column>
      <el-table-column prop="name" label="微信号" width="150">
      </el-table-column>
      <el-table-column prop="name" label="基地名称" width="180">
      </el-table-column>
			<el-table-column prop="name" label="账户状态" min-width="120">
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template slot-scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar" style="margin-top: 25px;">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="20" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm" style="display: table;width: 100%;">
        <div style="float: left;width: 45%;">
          <el-form-item label="登录账号" prop="name">
          	<el-input v-model="editForm.name" placeholder="请输入手机号码"></el-input>
          </el-form-item>
          <el-form-item label="登录logo" prop="name">
          	<el-input v-model="editForm.name" placeholder="请输入手机号码"></el-input>
          </el-form-item>
          <el-form-item label="登录密码" prop="name">
          	<el-input v-model="editForm.name" type="password" placeholder="请输入手机号码"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="name">
          	<el-input v-model="editForm.name" type="password" placeholder="请输入手机号码"></el-input>
          </el-form-item>
        </div>
        <div style="float: left;width: 45%;margin-left: 5%;">
          <el-form-item label="联系人" prop="name">
          	<el-input v-model="editForm.name" placeholder="请输入手机号码"></el-input>
          </el-form-item>
          <el-form-item label="微信号" prop="name">
          	<el-input v-model="editForm.name" placeholder="请输入手机号码"></el-input>
          </el-form-item>
          <el-form-item label="基地名称" prop="name">
          	<el-input v-model="editForm.name" placeholder="请输入手机号码"></el-input>
          </el-form-item>
          <el-form-item label="电子邮箱" prop="name">
          	<el-input v-model="editForm.name" placeholder="请输入手机号码"></el-input>
          </el-form-item>
          <el-form-item label="账户状态">
          	<el-radio-group v-model="editForm.radio1">
          	    <el-radio :label="3">启用</el-radio>
          	    <el-radio :label="6">禁用</el-radio>
          	  </el-radio-group>
          </el-form-item>
        </div>

			</el-form>
			<div slot="footer" class="dialog-footer">
			 <el-button @click.native="dialogFormVisible=false">取消</el-button>
			  <el-button v-if="dialogStatus=='create'" type="primary">添加</el-button>
        <el-button v-else type="primary">修改</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
// import util from '@/utils/table.js'
import {
  removeUser,
  batchRemoveUser
} from '@/api/userTable'

export default {
  data() {
    return {
      dialogStatus: '',
      textMap: {
        update: '编辑账户',
        create: '添加账户'
      },
      dialogFormVisible: false,
      filters: {
        region: '',
        phone: '',
        name: ''
      },
      users: [],
      total: 0,
      page: 1,
      sels: [], // 列表选中列
      editFormRules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }]
      },
      // 编辑界面数据
      editForm: {
        id: '0',
        name: '',
        radio1: ''
      },
      addFormVisible: false, // 新增界面是否显示
      addFormRules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }]
      }
    }
  },
  methods: {
    onUserList() {
      var list = [{
        name: 1,
        shopId: 2,
        img: '../static/img/img.jpg',
        radio1: 3
      }]
      this.users = list
      this.total = list.length
    },
    handleCurrentChange(val) {
      this.page = val
      this.getUsers()
    },
    // 获取用户列表
    // getUsers() {
    //   const para = {
    //     page: this.page,
    //     name: this.filters.name
    //   }
    //   getUserListPage(para).then(res => {
    //     this.total = res.data.total
    //     this.users = res.data.users
    //   })
    // },
    // 删除
    handleDel(index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      })
        .then(() => {
          const para = { id: row.id }
          removeUser(para).then(res => {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.getUsers()
          })
        })
        .catch(() => {})
    },
    // 显示编辑界面
    handleEdit(index, row) {
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.editForm = Object.assign({}, row)
      console.log(Object.assign({}, row))
    },
    // 显示新增界面
    handleAdd() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.editForm = {
        id: '0',
        name: '',
        radio1: 3
      }
    },
    // 编辑
    // updateData() {
    //   this.$refs.editForm.validate(valid => {
    //     if (valid) {
    //       this.$confirm('确认提交吗？', '提示', {})
    //         .then(() => {
    //           const para = Object.assign({}, this.editForm)
    //           para.birth =
    //             !para.birth || para.birth === ''
    //               ? ''
    //               : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd')
    //           editUser(para).then(res => {
    //             this.$message({
    //               message: '提交成功',
    //               type: 'success'
    //             })
    //             this.$refs['editForm'].resetFields()
    //             this.dialogFormVisible = false
    //             this.getUsers()
    //           })
    //         })
    //         .catch(e => {
    //           // 打印一下错误
    //           console.log(e)
    //         })
    //     }
    //   })
    // },
    // 新增
    // createData: function() {
    //   this.$refs.editForm.validate(valid => {
    //     if (valid) {
    //       this.$confirm('确认提交吗？', '提示', {})
    //         .then(() => {
    //           this.editForm.id = (parseInt(Math.random() * 100)).toString() // mock a id
    //           const para = Object.assign({}, this.editForm)
    //           console.log(para)

    //           para.birth =
    //             !para.birth || para.birth === ''
    //               ? ''
    //               : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd')
    //           addUser(para).then(res => {
    //             this.$message({
    //               message: '提交成功',
    //               type: 'success'
    //             })
    //             this.$refs['editForm'].resetFields()
    //             this.dialogFormVisible = false
    //             this.getUsers()
    //           })
    //         })
    //         .catch(e => {
    //           // 打印一下错误
    //           console.log(e)
    //         })
    //     }
    //   })
    // },
    // 全选单选多选
    selsChange(sels) {
      this.sels = sels
    },
    // 批量删除
    batchRemove() {
      var ids = this.sels.map(item => item.id).toString()
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      })
        .then(() => {
          const para = { ids: ids }
          batchRemoveUser(para).then(res => {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.getUsers()
          })
        })
        .catch(() => {})
    }
  },
  mounted() {
    this.onUserList(1)
  }
}
</script>

<style scoped>

</style>
